﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.IO;
using System.Data;
using System.Web.UI;
using System.Threading;
using System.Web;

namespace MyLib.Utils
{
    public  partial class WebUtil
    {
        public static void ExecuteClientScript(Page page, string clientScript)
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), Guid.NewGuid().ToString(), 
                clientScript,true);
        }

        public static string GetCurrentLanguage()
        {
            return Thread.CurrentThread.CurrentUICulture.Name;
        }

        static void ChangeLanguage(string lang)
        {
            System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(lang);
            System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(lang);
        }

        public static List<Control> GetControls<T>(ControlCollection ctrls)
        {
            List<System.Web.UI.Control> list = new List<System.Web.UI.Control>();
            foreach (System.Web.UI.Control ctrl in ctrls)
            {
                if (ctrl is T)
                {
                    list.Add(ctrl);
                }
                if (ctrl.Controls.Count > 0)
                {
                    list.AddRange(GetControls<T>(ctrl.Controls));                    
                }
            }
            return list;
        }

        public static string GetRequestWebSite()
        {
            //if (HttpContext.Current.Request.ApplicationPath.Trim() != "/")
            //{
            //    return HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath;
            //}
            //else
            //{
                return HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority;
            //}
        }
    }
}
